package cn.wolfcode.controller;

import cn.wolfcode.domain.Notelist;
import cn.wolfcode.domain.User;
import cn.wolfcode.redis.IUserInfoRedisService;
import cn.wolfcode.service.INotelistService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 帖子列表控制层
 */
@RestController
@RequestMapping("/notelist")
public class NotelistController {

    @Autowired
    private INotelistService notelistService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @GetMapping("/list")
    public Object list() {
        List<Notelist> list = notelistService.list();
        return JsonResult.success(list);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object saveOrUpdate(String notedetialTitle,String reply, HttpServletRequest request) {
        // 获取用户对象
        // 登录拦截器,登录了才可以操作
        String token = request.getHeader("token");
        User user = userInfoRedisService.getUserByToken(token);
        if (user ==null){
            return JsonResult.error(JsonResult.CODE_NOLOGIN,"请先登录",null);
        }
        Notelist notelist = new Notelist();
        notelist.setNotedetialTitle(notedetialTitle);

        notelist.setUserId(user.getId());
        notelistService.insert(notelist);
        return JsonResult.success();
    }
}
